package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication1.dao.UserDao;
import com.example.myapplication1.model.User;

public class RegActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_save;
    private Button btn_reset;
    private Button btn_back;
    private EditText et_account;
    private EditText et_password;
    private EditText et_password_cfm;
    private EditText et_phone;

    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        btn_save=findViewById(R.id.btn_save);
        btn_save.setOnClickListener(this);
        btn_reset=findViewById(R.id.btn_reset);
        btn_reset.setOnClickListener(this);
        btn_back=findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);

        et_account=findViewById(R.id.et_account);
        et_password=findViewById(R.id.et_password);
        et_password_cfm=findViewById(R.id.et_password_cfm);
        et_phone=findViewById(R.id.et_phone);

        userDao=new UserDao(RegActivity.this);
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.btn_save:
                //1.获取输入框中的内容
                String account=et_account.getText().toString();
                String password=et_password.getText().toString();
                String password_cfm=et_password_cfm.getText().toString();
                String phone=et_phone.getText().toString();
                //判断内容是否合法
                //account.trim():表示去除左右空格
                if (account.trim().length()==0){
                    Toast.makeText(RegActivity.this,"账号不能为空", Toast.LENGTH_SHORT).show();
                    return;//中断方法
                }
                if (password.length()==0){
                    Toast.makeText(RegActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password.equals(password_cfm)){
                    Toast.makeText(RegActivity.this,"密码不一致",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (phone.trim().length()==0){
                    Toast.makeText(RegActivity.this,"手机号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                //判断账号和手机号是否重复
                if(userDao.isExistByAccountOrPhone(account,phone)){
                    Toast.makeText(RegActivity.this,"账号或手机号已存在，请重新输入",Toast.LENGTH_SHORT).show();
                    return;
                }

                //将数据打包到User对象中
                User bean=new User(account,password,phone);
                //添加到集合中
                userDao.insert(bean);
                //返回到登录界面
                finish();
                break;
            case R.id.btn_reset:
                //清空输入框中内容
                et_account.setText("");
                et_password.setText("");
                et_password_cfm.setText("");
                et_password.setText("");
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }
}